import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../models/family_member.dart';
import '../providers/family_providers.dart';

/// 心情选择器对话框
class MoodSelectorDialog extends ConsumerWidget {
  /// 构造函数
  const MoodSelectorDialog({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final family = ref.watch(familyProvider);
    final currentUser = family?.currentUser;
    
    if (family == null || currentUser == null) {
      return const AlertDialog(
        title: Text('错误'),
        content: Text('无法获取用户信息'),
      );
    }
    
    return AlertDialog(
      title: const Text('设置我的心情'),
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(
            '你好，${currentUser.name}',
            style: const TextStyle(fontWeight: FontWeight.bold),
          ),
          const SizedBox(height: 8),
          const Text('请选择你当前的心情状态：'),
          const SizedBox(height: 16),
          // 心情选择网格
          GridView.builder(
            shrinkWrap: true,
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 3,
              childAspectRatio: 1.0,
              crossAxisSpacing: 10.w,
              mainAxisSpacing: 10.h,
            ),
            itemCount: MoodStatus.values.length,
            itemBuilder: (context, index) {
              final mood = MoodStatus.values[index];
              final isSelected = currentUser.mood == mood;
              
              return InkWell(
                onTap: () => _selectMood(context, ref, mood),
                child: Container(
                  decoration: BoxDecoration(
                    color: isSelected ? mood.color.withOpacity(0.2) : Colors.transparent,
                    border: Border.all(
                      color: isSelected ? mood.color : Colors.grey.withOpacity(0.3),
                      width: isSelected ? 2 : 1,
                    ),
                    borderRadius: BorderRadius.circular(8.r),
                  ),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Icon(
                        mood.icon,
                        color: mood.color,
                        size: 32.sp,
                      ),
                      SizedBox(height: 4.h),
                      Text(
                        mood.displayName,
                        style: TextStyle(
                          color: isSelected ? mood.color : Colors.black87,
                          fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
                        ),
                      ),
                    ],
                  ),
                ),
              );
            },
          ),
        ],
      ),
      actions: [
        TextButton(
          onPressed: () => Navigator.pop(context),
          child: const Text('取消'),
        ),
      ],
    );
  }

  /// 选择心情
  void _selectMood(BuildContext context, WidgetRef ref, MoodStatus mood) {
    final family = ref.read(familyProvider);
    final currentUser = family?.currentUser;
    
    if (family == null || currentUser == null) return;
    
    ref.read(familyProvider.notifier).updateCurrentUserMood(mood);
    Navigator.pop(context);
    
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text('心情已更新为：${mood.displayName}')),
    );
  }
}
